struct Light
{
	float4 direction;
	float4 colour;
	float4 ambient;
	float4 position;
	float4 attenuation;
	float2 pad1;
	float range;
	int enabled;
};

cbuffer ConstantBuffer : register( b0 )
{
	float4 TEST;
	matrix World;
	matrix View;
	matrix Projection;
	matrix LightViewMatrix;
	matrix LightProjMatrix;
	Light light[3];
	float4 OutputColour;
	float2 TexturePositionOne;
	float2 TexturePositionTwo;
	float3 CameraPosition;
}
//--------------------------------------------------------------------------------------
struct VS_INPUT // = PS_INPUT
{
    float4 Pos : POSITION;
    float2 TexCoord : TEXCOORD0;
	float3 Normal : NORMAL0;
	float3 Tangent : TANGENT;
	float3 Binormal : BINORMAL;
};

struct PS_INPUT
{
	float4 Pos : SV_POSITION;
	float2 TexCoord : TEXCOORD0;
	float3 Normal : NORMAL0;
	float3 Tangent : TANGENT;
	float3 Binormal : BINORMAL;
	float4 HeightMapPos : TEXCOORD1;
	float4 LightViewPosition : TEXCOORD2;
	float4 RefractionPosition : TEXCOORD3;
	float4 ViewDirection : TEXCOORD4;
};


Texture2D ObjTexture : register( t0 );
Texture2D ObjTexture2 : register( t1 );
Texture2D ObjTexture3 : register( t2 );
Texture2D ObjTexture4 : register( t3 );
Texture2D ObjTexture5 : register( t4 );
SamplerState ObjSamplerState : register( s0 );

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS(VS_INPUT input)
{
    PS_INPUT output = (PS_INPUT)0;
	
	//output.Pos = LightDirection[0];


    output.Pos = mul( input.Pos, World );
    output.Pos = mul( output.Pos, View );
    output.Pos = mul( output.Pos, Projection );
	
	output.Normal = mul(input.Normal, World);
	
	output.TexCoord = input.TexCoord;
	output.HeightMapPos = mul(input.Pos, World);
	
	output.RefractionPosition = mul(input.Pos, World);
	output.RefractionPosition = mul(output.RefractionPosition, View);
	output.RefractionPosition = mul(output.RefractionPosition, Projection);
	
	float3 worldPos =  mul(input.Pos, World);
	output.ViewDirection = float4(CameraPosition - worldPos, 1.0f);
	output.ViewDirection = normalize(output.ViewDirection);

    return output;
}

float4 PS(PS_INPUT input) : SV_Target
{
	float4 finalColour = 0;
	float2 translatedTextureCoord = 0;
	translatedTextureCoord = input.TexCoord;
	translatedTextureCoord.x += TexturePositionOne.x;
	float2 translatedTextureCoord2 = 0;
	translatedTextureCoord2 = input.TexCoord;
	translatedTextureCoord2.x += TexturePositionOne.x * 3.0f;

	// refraction calcs
	float2 refractionTexCoord = 0;
	refractionTexCoord.x = input.RefractionPosition.x / input.RefractionPosition.w / 2.0f + 0.5f;
	refractionTexCoord.y = -input.RefractionPosition.y / input.RefractionPosition.w / 2.0f + 0.5f;
	
	float4 refractionTexture =  ObjTexture.Sample(ObjSamplerState, refractionTexCoord);
	float4 waterTexture = ObjTexture3.Sample(ObjSamplerState, (translatedTextureCoord * 32.0f) + 1.0f);
	float4 normalMap = ObjTexture4.Sample(ObjSamplerState, (translatedTextureCoord * 16.0f));
	float4 normalMap2 = ObjTexture4.Sample(ObjSamplerState, (translatedTextureCoord * 4.0f));
	float4 reflectionTexture = ObjTexture2.Sample(ObjSamplerState, input.TexCoord);
	
	float4 normal = normalMap + normalMap2;

	if (light[1].enabled)
	{
        finalColour = saturate(dot(light[1].direction, normal.xyz) * OutputColour);
    }
	
	float4 finalTexture = refractionTexture * reflectionTexture;

	finalColour += finalTexture;

	return finalColour;
}